﻿using System;
using System.Text;
using System.Net.Sockets;
using System.Net;
namespace Socket_PDA通信
{
    public class PdaClient
    {
        TcpClient client;
        public string name;
        public const int TimeInterval = 1000 * 40;
        public  int port = 8001;
        public string ipAdress;
        byte[] myReadBuffer = new byte[1024];
        public delegate void MessageCome(string str);
        public MessageCome OnMessageCome;

        public PdaClient()
        {

           
           
        }

    
        public void Send(string msg)
        {
            if (client.Client == null || client.Client.Connected == false)
            {
                connect();
                return;
            }
            byte[] sendbyte = new byte[100];
            sendbyte = Encoding.GetEncoding("gb2312").GetBytes(msg);
            try
            {
                int res = client.Client.Send(sendbyte);
            }
            catch
            {
                connect();
            }
        }
        public void connect()
        {
            IPEndPoint ipep = new IPEndPoint(IPAddress.Parse(ipAdress), port);
            try
            {
                if (client != null)
                {
                    try
                    {
                        client.Client.Shutdown(SocketShutdown.Both);
                        client.Client.Close();
                    }
                    catch
                    {

                    }
                }
                client = new TcpClient();
                client.Connect(ipep);
                GetRead();
                //Send("reg:" + this.name);
            }
            catch (Exception a)
            {

               
                return;
            }
        }
        void GetRead()
        {
            try
            {
                if (client.GetStream().CanRead)
                {
                    myReadBuffer = new byte[1024];
                    client.Client.BeginReceive(myReadBuffer, 0, myReadBuffer.Length, SocketFlags.None,
     new AsyncCallback(myReadCallBack), client.Client);
                }
            }
            catch (Exception a)
            {
               
            }
        }
        private void myReadCallBack(IAsyncResult ar)
        {

            try
            {
                Socket s = ar.AsyncState as Socket;

                if (client.Client.Connected == false)
                {
                    client.Close();
                }
                int length = client.Client.EndReceive(ar);
                if (length == 0)
                {
                    client.Client.Shutdown(SocketShutdown.Both);
                    client.Client.Close();
                    return;
                }
                if (client.Client.Connected == true && length > 0)
                {
                    string str = Encoding.GetEncoding("gb2312").
                             GetString(myReadBuffer, 0, length);
                    if (OnMessageCome != null)
                    {
                        OnMessageCome(str);
                    }
                    myReadBuffer = new byte[1024];

                    s.BeginReceive(myReadBuffer, 0, myReadBuffer.Length, SocketFlags.None,
     new AsyncCallback(myReadCallBack), client.Client);
                }
                else
                {

                    s.Close();
                    return;
                }
            }
            catch (Exception a)
            {
               
                return;
            }


        }
    }
}
